/************************************************************************/
/*Copyright (c) James Zhao 2007,2008
/*This file is part of AdminOverlay.
/*AdminOverlay is free software: you can redistribute it and/or modify
/*it under the terms of the GNU General Public License as published by
/*the Free Software Foundation, either version 3 of the License, or
/*(at your option) any later version.
/*
/*AdminOverlay is distributed in the hope that it will be useful,
/*but WITHOUT ANY WARRANTY; without even the implied warranty of
/*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/*GNU General Public License for more details.
/*
/*You should have received a copy of the GNU General Public License
/*along with Foobar.  If not, see <http://www.gnu.org/licenses/>.                                                                     */
/*************************************************************************/
#include "UIButton.h"


UIButton::UIButton( int x , int y , int width, int height  )
	:UIPanel(x,y,width,height)
{
	TopColor(0xC7232721);
	BottomColor(0xC7232721);

	lineBuffer = new LineBuffer(1);

	borderTop = new LineVisual(0xC75D5D5D);
	borderBottom = new LineVisual(0xC75D5D5D);
	borderLeft = new LineVisual(0xC75D5D5D);
	borderRight = new LineVisual(0xC75D5D5D);
	
	lineBuffer->addLine(borderTop);
	lineBuffer->addLine(borderBottom);
	lineBuffer->addLine(borderLeft);
	lineBuffer->addLine(borderRight);

	connect(this, SIGNAL(mouseMoved(QPoint, bool, bool)), 
		SLOT(onMouseMove(QPoint, bool, bool)), Qt::DirectConnection);
	connect(this, SIGNAL(mouseDown(QPoint, bool)), 
		SLOT(onMouseDown(QPoint, bool)), Qt::DirectConnection);
	connect(this, SIGNAL(mouseUp(QPoint, bool)), 
		SLOT(onMouseUp(QPoint, bool)), Qt::DirectConnection);

	State(UIButtonState_Normal);
	NeedUpdate(true);
}
UIButton::~UIButton(void)
{
}
#include "debug.h"
void UIButton::onMouseMove( QPoint point, bool isMouseDown, bool isPrimaryTarget )
{
	if(isPrimaryTarget)
	{
		if(isMouseDown == false)
		{
			State(UIButtonState_Hovering);
		}
	}
	else
	{
		State(UIButtonState_Normal);
	}
}

void UIButton::onMouseDown( QPoint point, bool isPrimaryTarget )
{
	if(isPrimaryTarget)
	{
		State(UIButtonState_Pressed);
	}
}

void UIButton::onMouseUp( QPoint point, bool isPrimaryTarget )
{
	if(isPrimaryTarget)
	{
		State(UIButtonState_Hovering);
	}
	else
	{
		State(UIButtonState_Normal);
	}
}

void UIButton::onDeviceReset()
{
	lineBuffer->onDeviceReset();
}

void UIButton::draw( LPDIRECT3DDEVICE9 device )
{
	if(stateChanged)
	{
		switch(state)
		{
		case UIButtonState_Normal:
			TopColor(0xC7232721);
			BottomColor( 0xC7232721);
			//LogDebug("layout normal \n");
			break;
		case UIButtonState_Hovering:
			TopColor(0xC73F453A);
			BottomColor( 0xC73F453A);
			//LogDebug("layout hovering \n");
			break;
		case UIButtonState_Pressed:
			TopColor(0xC7171A15);
			BottomColor( 0xC7171A15);
			break;
		}
		stateChanged = false;
	}
	UIPanel::draw(device);
	lineBuffer->draw(device);
}

void UIButton::layout(QPoint offset)
{
	UIPanel::layout(offset);
	bool update = NeedUpdate();
	if(update == true)
	{
		borderTop->Point1(GlobalPos());
		borderTop->Point2(GlobalX() + Width(),  GlobalY());

		borderBottom->Point1(GlobalX(), GlobalY() + Height());
		borderBottom->Point2(GlobalX() + Width(),  GlobalY() + Height());

		//the +1 fixes some drawing issues
		borderLeft->Point1(GlobalPos());
		borderLeft->Point2(GlobalX() , GlobalY() + Height());

		borderRight->Point1(GlobalX() + Width(), GlobalY());
		borderRight->Point2(GlobalX() + Width(), GlobalY() + Height());
		
		NeedUpdate(false);
	}
}

void UIButton::State( UIButtonState val )
{
	if(val != state)
	{
		state = val; 
		stateChanged = true;
		//NeedUpdate(true);	
		//LogDebug("state changed \n");
	}
}

UIButtonState UIButton::State() const
{
	return state;
}

void UIButton::NeedUpdate(bool val)
{
	UIPanel::NeedUpdate(val);
}